package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.Building;
import com.example.studentstatusmanagement.pojo.BuildingExample;
import com.example.studentstatusmanagement.pojo.Classroom;
import com.example.studentstatusmanagement.pojo.ClassroomExample;
import com.example.studentstatusmanagement.pojo.doo.ReqRoom;
import com.example.studentstatusmanagement.pojo.dto.SRoom;
import com.example.studentstatusmanagement.service.BuildingService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/building")
public class BuildingController {


    @Autowired
    private BuildingService buildingService;

    @RequestMapping("show")
    public Map<String, Object> showMajorsInfo(ReqRoom room) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SRoom> ls = buildingService.selectBuildingsInfo(room);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }

    @RequestMapping("add")
    public ResultResponse add(Building building) {

        BuildingExample example = new BuildingExample();
        BuildingExample.Criteria c = example.createCriteria();
        c.andBuildingnameEqualTo(building.getBuildingname());

        if (buildingService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (buildingService.insertSelective(building) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("del")
    public ResultResponse del(Building building) {
        if (!StringUtils.isEmpty(building.getId())) {
            building.setIsdel(SystemVariable.IS_DEL);
            if (buildingService.updateByPrimaryKeySelective(building) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("use")
    public ResultResponse use(Building building) {
        if (!StringUtils.isEmpty(building.getId())) {
            building.setIsdel(SystemVariable.IS_NOT_DEL);
            if (buildingService.updateByPrimaryKeySelective(building) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("update")
    public ResultResponse update(Building building) {
        BuildingExample example = new BuildingExample();
        BuildingExample.Criteria c = example.createCriteria();
        c.andBuildingnameEqualTo(building.getBuildingname());
        c.andIdNotEqualTo(building.getId());
        if (buildingService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (!StringUtils.isEmpty(building.getId())) {
            if (buildingService.updateByPrimaryKeySelective(building) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
}
